class Login {
    constructor() {
        //用户名
        this.ri= document.querySelector('reg_input');
        //密码
        this.ps = document.querySelector('reg_code');
        //登录按钮
        this.button = document.querySelector('button');
        //去注册
        // this.go = document.querySelector('#go');
        //数组
        this.arr = [false, false];
        //添加事件
        this.addEvent();
    }
    addEvent() {
        //记录this
        let that = this;
        //前端验证
        this.ri.onblur = function () {
            //正则
            let re = /^[\u4e00-\u9fa5]{3,}$/;
            //获取用户名
            let ri = this.value;
            //检测
            if (re.test(ri)) {
                that.ipo.style.display = 'none';
                that.arr[0] = true;
            } else {
                that.ipo.style.display = 'block';
                // that.arr[0] = false;
            }
        }
        this.ps.onblur = function () {
            //正则
            let re = /^\w{3,12}$/;
            //获取用户名
            let ps = this.value;
            //检测
            if (re.test(ps)) {
                this.style.backgroundColor = 'green';
                that.arr[1] = true;
            } else {
                this.style.backgroundColor = 'red';
                that.arr[1] = false;
            }
        }
        this.button.onclick = function () {
            if (that.arr.includes(false)) {
                alert('请完善登录信息！');
                return;
            } else {
                //后端验证
                //获取cookie
                let cookie_str = $.getCookie('users') ? $.getCookie('users') : '';
                //转对象
                let cookie_obj = $.convertStrToObj(cookie_str);
                //获取当前用户名
                let ri = that.ri.value;
                //获取当前密码
                let ps = that.ps.value;
                //检测
                if (ri in cookie_obj) {
                    //用户有
                    if (ps === cookie_obj[ri]) {
                        alert('登录成功！');
                        location.href = 'product.html';
                        //创建cookie，将登录成功的用户名存储到cookie中，实现7天免登录
                        $.cookie('logined', ri, { expires: 7, path: '/' });
                    } else {
                        alert('密码错误！');
                    }
                } else {
                    alert('用户名不存在！');
                }
            }
        }
        this.button.onclick = function () {
            location.href = 'https://www.yougou.com/';
        }
    }
}
new Login();